package com.tyk.bookstore.back.system.service.impl;


import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.system.model.entity.UserDo;
import com.tyk.bookstore.back.system.model.result.UserResultEnum;
import com.tyk.bookstore.back.system.repository.UserRepository;
import com.tyk.bookstore.back.system.service.UserDetailApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 用户管理对内暴露接口
 */
@Service
@RequiredArgsConstructor
public class UserDetailApiServiceImpl implements UserDetailApiService {

    private final UserRepository userRepository;

    @Override
    public List<BinVo> getNameVoListByIdSet(Set<Long> idSet) {
        return userRepository
                .<Long, String, Long>selectBinList1In(
                        UserDo::getId, UserDo::getName,
                        UserDo::getId, idSet,
                        UserDo.class
                )
                .stream().map(it -> new BinVo(it.getV1().longValue(), it.getV2())).toList();
    }

    // TODO
    @Override
    public void assertExistById(Long id) {
        AssertUtil.isTrue(
                userRepository.exist1Eq(UserDo::getId, id, UserDo.class),
                UserResultEnum.USER_NOT_FOUND
        );
    }

    // TODO
    @Override
    public String getNameById(Long id) {
        String name = userRepository.<String>selectSin1Eq(
                UserDo::getName,
                UserDo::getId, id,
                UserDo.class
        ).getV1();
        AssertUtil.notBlank(name, UserResultEnum.USER_NOT_FOUND);
        return name;
    }

    @Override
    public List<Long> getIdListByNameLike(String name) {
        return userRepository.<BigInteger>selectSinList1Like(
                UserDo::getId,
                UserDo::getName, name,
                UserDo.class
        ).stream().map(it -> it.getV1().longValue()).toList();
    }
}
